//
//  SettingViewController.m
//  MyLittleBrother
//
//  Created by speeda on 17/8/1.
//  Copyright © 2017年 speeda. All rights reserved.
//

#import "SettingViewController.h"
#import "NotifyTableViewController.h"
#import "IdeaViewController.h"
#import "AboutViewController.h"
#import "LoginViewController.h"

@interface SettingViewController () <UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *seetingTableView;
@property (nonatomic, strong) NSArray *seetingArray;
@property (nonatomic, strong) NSArray *subControllerArray;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initView];
}

- (void)initView {
    
    self.title = @"设置";
    self.seetingTableView.dataSource = self;
    self.seetingTableView.delegate = self;
    self.seetingTableView.estimatedRowHeight = 100;
    self.seetingTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.seetingTableView.rowHeight = UITableViewAutomaticDimension;
    self.seetingTableView.tableFooterView = [[UIView alloc] init];
    self.seetingTableView.contentInset = UIEdgeInsetsMake(20, 0, 0, 0);
    
}


#pragma mark -- 退出登录
- (IBAction)leaveBtnAction:(id)sender {
    
    NSString*appDomain = [[NSBundle mainBundle] bundleIdentifier];
    [[NSUserDefaults standardUserDefaults] removePersistentDomainForName:appDomain];
    
    LoginViewController *loginVc = [[LoginViewController alloc] init];
    [self.navigationController pushViewController:loginVc animated:YES];
}



#pragma maek -- UITableViewDataSource,UITableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.seetingArray.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *settingCell = @"settingCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:settingCell];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:settingCell];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    if (indexPath.row == 3) {
        
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM",[self readCacheSize]];
        
    } else {
    
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    
    
    cell.textLabel.text = self.seetingArray[indexPath.row];
    
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (indexPath.row == 3) {
        
        UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.2fM",[self clearFile]];
        
    } else {
    
        UIViewController *Vc = self.subControllerArray[indexPath.row];
        [self.navigationController pushViewController:Vc animated:YES];
    }
    
}


#pragma mark -- 清除缓存
// 获取缓存文件的大小
-( CGFloat )readCacheSize
{
    NSString *cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES) firstObject];
    return [self folderSizeAtPath :cachePath];
    
}


// 遍历文件夹获得文件夹大小，返回多少 M
- ( CGFloat ) folderSizeAtPath:( NSString *) folderPath{
    
    NSFileManager * manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath :folderPath]) return 0 ;
    NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator];
    NSString * fileName;
    long long folderSize = 0 ;
    while ((fileName = [childFilesEnumerator nextObject]) != nil ){
        //获取文件全路径
        NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName];
        folderSize += [ self fileSizeAtPath :fileAbsolutePath];
    }
    
    return folderSize/( 1024.0 * 1024.0);
    
}



// 计算 单个文件的大小
- ( long long ) fileSizeAtPath:( NSString *) filePath{
    NSFileManager * manager = [NSFileManager defaultManager];
    if ([manager fileExistsAtPath :filePath]){
        return [[manager attributesOfItemAtPath :filePath error : nil] fileSize];
    }
    return 0;
}


// 清除缓存
- (CGFloat )clearFile
{
    NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject];
    NSArray * files = [[NSFileManager defaultManager ] subpathsAtPath :cachePath];
    //NSLog ( @"cachpath = %@" , cachePath);
    for ( NSString * p in files) {
        
        NSError * error = nil ;
        //获取文件全路径
        NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent :p];
        
        if ([[NSFileManager defaultManager ] fileExistsAtPath :fileAbsolutePath]) {
            [[NSFileManager defaultManager ] removeItemAtPath :fileAbsolutePath error :&error];
        }
    }
    
    //读取缓存大小
    CGFloat cacheSize = [self readCacheSize] * 1024;
    
    return cacheSize;
    
}


- (NSArray *)seetingArray {
    
    if (!_seetingArray) {
        _seetingArray = @[@"新消息通知",@"意见反馈",@"关于",@"清除缓存"];
    }
    
    return _seetingArray;
}


- (NSArray *)subControllerArray {
    
    if (!_subControllerArray) {
        
        _subControllerArray = @[[[NotifyTableViewController alloc] init],[[IdeaViewController alloc] init],[[AboutViewController alloc] init]];
    }
    
    return _subControllerArray;
}

@end
